<!--
 * @Description: 商家远程项目管理列表
 * @Author: liyujie
 * @Date: 2021/08/02
 * @Last Modified by: liyujie
 * @Last Modified time: 2021/08/02
-->
<template>
    <section class="wrapper">
        <div class="content-wrapper">
            <!-- header -->
            <div class="header">
                <div class="header-title">
                    项目管理列表
                </div>
            </div>
            <!-- /header -->
            <!-- 筛选-->
            <div class="filter-view">
                <div class="filter-left">
                    <div class="filter-item">
                        <el-radio-group
                            v-model="filterData.status"
                            size="medium"
                            @change="getTableData(1)"
                        >
                            <el-radio-button
                                v-for="item in statusList"
                                :key="item.value"
                                :label="item.value"
                            >
                                {{ item.label }}
                            </el-radio-button>
                        </el-radio-group>
                    </div>
                    <div class="filter-item">
                        <p class="filter-item__title">
                            项目名称
                        </p>
                        <el-select
                            v-model="filterData.keyword"
                            filterable
                            clearable
                            size="medium"
                            placeholder="请选择项目名称"
                            style="width: 260px"
                            @change="getTableData(1)"
                        >
                            <el-option
                                v-for="item in project_list"
                                :key="item.shop_project_no"
                                :label="item.project_title"
                                :value="item.project_title"
                            />
                        </el-select>
                    </div>
                    <div class="filter-item">
                        <el-button
                            type="primary"
                            size="small"
                            class="query-button"
                            @click="getTableData(1)"
                        >
                            查询
                        </el-button>
                        <el-button
                            type="text"
                            @click="handleReset()"
                        >
                            重置
                        </el-button>
                    </div>
                </div>
                <div class="filter-right">
                    <el-button
                        type="primary"
                        size="medium"
                        style="margin-top: -24px"
                        @click="showDialog('add')"
                    >
                        创建项目类型
                    </el-button>
                </div>
            </div>
            <!--筛选-->
            <!-- table -->
            <div class="table-wrapper">
                <el-table
                    :data="tableData"
                    height="100%"
                    style="width: 100%;"
                    ref="table"
                    class="data-table"
                    :cell-style="{height: '60px'}"
                    v-loading="tableLoading"
                >
                    <el-table-column
                        label="序号"
                        align="center"
                        width="80"
                        type="index"
                        fixed="left"
                    />
                    <el-table-column
                        prop="project_title"
                        label="项目名称"
                        :formatter="emptyFormatter"
                    />
                    <el-table-column
                        prop="project_logo"
                        label="项目logo"
                        align="center"
                    >
                        <template slot-scope="scope">
                            <img
                                class="project-logo"
                                :src="scope.row.project_logo"
                                alt=""
                            >
                        </template>
                    </el-table-column>
                    <el-table-column
                        prop="project_intro"
                        label="项目介绍"
                        width="140"
                        show-overflow-tooltip
                        :formatter="emptyFormatter"
                    />
                    <el-table-column
                        prop="task_price"
                        label="任务单价"
                        align="center"
                        :formatter="emptyFormatter"
                    >
                        <template slot-scope="scope">
                            ￥{{ scope.row.task_price }}
                        </template>
                    </el-table-column>
                    <el-table-column
                        prop="student_week_receive_num"
                        label="每周限领(次)"
                        align="center"
                        :formatter="emptyFormatter"
                    />
                    <el-table-column
                        label="项目状态"
                        align="center"
                    >
                        <template slot-scope="scope">
                            <div class="status-wrapper">
                                <div
                                    class="status-dot"
                                    :class="['status-dot', scope.row.status === 1 ? 'success' : 'danger']"
                                />
                                <div>{{ scope.row.status | statusFilter }}</div>
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column
                        label="APP展示"
                        align="center"
                    >
                        <template slot-scope="scope">
                            <el-switch
                                v-model="scope.row.show_status"
                                :active-value="1"
                                :inactive-value="0"
                                @change="setShowStatus($event, scope.row)"
                            />
                        </template>
                    </el-table-column>
                    <el-table-column
                        prop="shop_apply_count"
                        label="商家报名数"
                        align="center"
                        :formatter="emptyFormatter"
                    />
                    <el-table-column
                        prop="used_count"
                        label="项目使用中数量"
                        align="center"
                        :formatter="emptyFormatter"
                    />
                    <el-table-column
                        label="操作"
                        align="left"
                        fixed="right"
                        width="292"
                    >
                        <template slot-scope="scope">
                            <div class="option-btn-group">
                                <el-button
                                    type="text"
                                    size="medium"
                                    :disabled="scope.row.status === 1"
                                    @click="showDialog('edit', scope.row)"
                                >
                                    编辑模版
                                </el-button>
                                <el-button
                                    type="text"
                                    size="medium"
                                    :disabled="scope.row.status === 1"
                                    @click="handleRoute('shopProjectEdit', {shop_project_no: scope.row.shop_project_no})"
                                >
                                    项目编辑
                                </el-button>
                                <el-button
                                    type="text"
                                    size="medium"
                                    @click="handleRoute('shopProjectDetail', {
                                        shop_project_no: scope.row.shop_project_no,
                                        project_title: scope.row.project_title
                                    })"
                                >
                                    详情
                                </el-button>
                                <el-button
                                    type="text"
                                    size="medium"
                                    @click="preSetStatus(scope.row)"
                                >
                                    {{ scope.row.status ? '下架' : '上架' }}
                                </el-button>
                                <el-button
                                    type="text"
                                    size="medium"
                                    :style="{color: scope.row.status ? '#C0C4CC' : '#F95B56'}"
                                    :disabled="scope.row.status === 1"
                                    @click="handleDelete(scope.row)"
                                >
                                    删除
                                </el-button>
                            </div>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <!-- /table -->
            <!-- /分页 -->
            <div class="pagination-wrapper clearfix">
                <el-pagination
                    layout="total, prev, pager, next, jumper"
                    background
                    :current-page.sync="page.now_page"
                    :total="page.total_count"
                    :page-size="page.page_size"
                    @current-change="getTableData"
                />
            </div>
        </div>
        <!--创建项目类型弹窗-->
        <el-dialog
            :visible.sync="dialog.dialogVisible"
            :title="dialog.mode === 'add' ? '创建项目类型' : '编辑项目类型'"
            :close-on-click-modal="false"
            width="660px"
            @close="handleDialogClose"
        >
            <div
                class="dialog-content"
                v-loading="dialog.loading"
            >
                <el-form
                    :model="ruleForm"
                    :rules="rules"
                    ref="ruleForm"
                    label-width="102px"
                >
                    <el-form-item
                        label="项目名称"
                        prop="project_title"
                    >
                        <el-input
                            v-model="ruleForm.project_title"
                            size="medium"
                            maxlength="12"
                            show-word-limit
                        />
                    </el-form-item>
                    <el-form-item
                        label="项目图标"
                        prop="project_logo"
                    >
                        <image-upload
                            v-model="ruleForm.project_logo"
                            :limit="20"
                            tips="图标大小120*120px，小于20kb"
                        />
                    </el-form-item>
                    <el-form-item
                        label="项目简介"
                        prop="project_intro"
                    >
                        <el-input
                            v-model="ruleForm.project_intro"
                            size="medium"
                            maxlength="15"
                            show-word-limit
                        />
                    </el-form-item>
                    <el-form-item
                        label="任务单价"
                        prop="task_price"
                    >
                        <el-input
                            v-model="ruleForm.task_price"
                            size="medium"
                            :disabled="dialog.mode === 'edit'"
                        >
                            <span
                                slot="suffix"
                                style="margin-right: 6px"
                            >元</span>
                        </el-input>
                    </el-form-item>
                    <el-form-item
                        label="每周限领数"
                        prop="student_week_receive_num"
                    >
                        <el-input-number
                            v-model="ruleForm.student_week_receive_num"
                            placeholder="请输入限领数"
                            :step="1"
                            step-strictly
                            :controls="false"
                            :min="1"
                            size="medium"
                            style="width: 223px"
                        />
                        <span class="form-tips">*用户每个自然周限制领取任务的数量</span>
                    </el-form-item>
                    <el-form-item
                        label="用户群体"
                        class="is-required"
                    >
                        <el-select
                            placeholder="全部"
                            v-model="ruleForm.stuagent_level"
                            placejolder="请选择"
                            size="medium"
                            style="width: 100%"
                            :disabled="dialog.mode === 'edit'"
                        >
                            <el-option
                                v-for="item in 5"
                                :key="item"
                                :label="`E${item - 1}以上（含E${item - 1}）`"
                                :value="item - 1"
                            />
                            <el-option
                                label="E5"
                                :value="5"
                            />
                        </el-select>
                    </el-form-item>
                </el-form>
            </div>
            <div class="dialog-footer">
                <el-button
                    size="small"
                    style="width: 100px"
                    @click="dialog.dialogVisible = false"
                >
                    取消
                </el-button>
                <el-button
                    type="primary"
                    size="small"
                    style="width: 100px;margin-left: 52px"
                    @click="handleSubmit"
                >
                    确定
                </el-button>
            </div>
        </el-dialog>
    </section>
</template>

<script>
import { back } from '@/mixin/back';
import ImageUpload from "@/components/common/image-upload";
import {deepClone} from "@/assets/js/utils";

export default {
    name: 'ShopProjectList',
    mixins: [back],
    components: {ImageUpload},
    data() {
        return {
            filterData: {
                keyword: '',
                status: 99
            },
            tableData: [],
            tableLoading: false,
            page: {
                now_page: 1,
                total_count: 0,
                page_size: 20
            },
            dialog: {
                loading: false,
                dialogVisible: false,
                mode: 'edit'
            },
            ruleForm: {
                shop_project_no: '',
                project_title: '',
                // 任务logo
                project_logo: '',
                // 任务简介
                project_intro: '',
                // 任务单价
                task_price: '',
                // 每周限领数量
                student_week_receive_num: undefined,
                // 精英等级
                stuagent_level: 0,
            },
            rules: {
                project_title: [
                    { required: true, message: '请输入项目名称', trigger: 'blur' }
                ],
                project_logo: [
                    { required: true, message: '请上次项目logo', trigger: 'change' }
                ],
                project_intro: [
                    { required: true, message: '请输入项目简介', trigger: 'blur' }
                ],
                task_price: [
                    { required: true, message: '请输入任务单价', trigger: 'blur' },
                    { pattern: /(?:^[1-9]([0-9]+)?(?:\.[0-9]{1,2})?$)|(?:^(?:0)$)|(?:^[0-9]\.[0-9](?:[0-9])?$)/, message: '任务单价必须为数字', trigger: 'blur' },
                ],
                student_week_receive_num: [
                    { required: true, message: '请输入每周限领数量', trigger: 'blur' }
                ]
            },
            statusList: [
                {label: '全部', value: 99},
                {label: '已上架', value: 1},
                {label: '已下架', value: 0},
            ],
            project_list: [],
            authResult: {}
        };
    },
    computed: {},
    filters: {
        statusFilter(val) {
            switch (val) {
                case 0:
                    return '已下架';
                case 1:
                    return '已上架';
                default:
                    return '--';
            }
        },
    },
    watch: {},
    mounted() {
        this.$store.commit('SET_HEADER', [{title: "APP后台"}, {title: '工作'}, {title: '商家远程项目'}, {title: '项目管理列表'}]);
        this.authResult = this.$route.meta.authResult;
        this.getTableData();
        this.getProjectList();
    },

    methods: {
        /**
         * @description 获取下拉数据
         */
        getProjectList() {
            this.$post('/student/task_stuagent_shop_project%5Cget_list_for_select', {}, res => {
                if (res.code === 1) {
                    this.project_list = res.data;
                }
            });
        },

        /**
         * @description 获取列表数据
         * @param page
         */
        getTableData(page) {
            this.tableLoading = true;
            let params = { ...this.filterData };

            params.page_id = page || this.page.now_page;
            this.$post('/student/task_stuagent_shop_project%5Cget_list', params, res => {
                if (res.code === 1) {
                    this.tableData = res.data.list;
                    this.page = res.data.page;
                } else {
                    this.$notify({
                        title: '提示',
                        message: res.msg,
                        type: 'warning'
                    });
                }
                this.tableLoading = false;
            });
        },

        /**
         * 路由跳转
         * @param routeName
         * @param query
         */
        handleRoute(routeName, query) {
            this.$router.push({
                name: routeName,
                query
            });
        },

        /**
         * @description 过滤函数
         */
        emptyFormatter(row, column, val) {
            if (!val) return '--';
            return val;
        },

        /**
         * 重置筛选条件
         */
        handleReset() {
            this.filterData = {
                keyword: '',
                status: 99
            };
            this.getTableData(1);
        },

        /**
         * 显示编辑弹窗
         * @param type
         * @param row
         */
        showDialog(type, row) {
            if (!this.authResult.auth_save) {
                this.$notify({
                    title: '提示',
                    message: '请联系管理员添加权限',
                    type: 'warning',
                    duration: 2000
                });
                return false;
            }
            this.dialog.mode = type;
            this.dialog.dialogVisible = true;
            if(row) {
                this.dialog.loading = true;
                this.$post('/student/task_stuagent_shop_project%5Cget', {
                    shop_project_no: row.shop_project_no
                }, res => {
                    if (res.code === 1) {
                        this.ruleForm = res.data;
                    } else {
                        this.$notify({
                            title: '提示',
                            message: res.msg,
                            type: 'warning'
                        });
                    }
                    this.dialog.loading = false;
                });

                this.ruleForm.shop_project_no = row.shop_project_no;
            } else {
                this.ruleForm = {
                    shop_project_no: '',
                    project_title: '',
                    project_logo: '',
                    project_intro: '',
                    task_price: '',
                    student_week_receive_num: undefined,
                    stuagent_level: 0,
                };
            }
        },

        /**
         * 上下架前操作
         * @param row
         */
        preSetStatus(row) {
            if (!this.authResult.auth_set_status) {
                this.$notify({
                    title: '提示',
                    message: '请联系管理员添加权限',
                    type: 'warning',
                    duration: 2000
                });
                return false;
            }
            if(row.status) {
                this.$zdConfirm(`下架后用户端${row.used_count ? `${row.used_count}个` : ''}任务将不再展示，请谨慎操作`, '下架项目', {
                    confirmButtonText: '确认下架',
                    cancelButtonText: '取消',
                    type: 'danger-warning'
                }).then(() => {
                    this.setStatus(row, 0);
                }).catch(() => {});
            } else {
                this.setStatus(row, 1);
            }
        },

        /**
         * 上下架请求
         * @param row
         * @param status
         */
        setStatus(row, status) {
            let params = {
                shop_project_no: row.shop_project_no,
                status,
            };

            this.$post('/student/task_stuagent_shop_project%5Cset_status', params, res => {
                if (res.code === 1) {
                    this.$notify({
                        title: '提示',
                        message: '操作成功',
                        type: 'success'
                    });
                    this.getTableData();
                    this.getProjectList();
                } else {
                    this.$notify({
                        title: '提示',
                        message: res.msg,
                        type: 'warning'
                    });
                }
            });
        },

        /**
         * 设置显示状态
         * @param row
         * @param show_status
         */
        setShowStatus(show_status, row) {
            let params = {
                shop_project_no: row.shop_project_no,
                show_status,
            };

            this.$post('/student/task_stuagent_shop_project%5Cset_show_status', params, res => {
                if (res.code === 1) {
                    this.$notify({
                        title: '提示',
                        message: '操作成功',
                        type: 'success'
                    });
                    this.getTableData();
                } else {
                    this.$notify({
                        title: '提示',
                        message: res.msg,
                        type: 'warning'
                    });
                }
            });
        },

        /**
         * 删除
         * @param row
         */
        handleDelete(row) {
            if (!this.authResult.auth_delete) {
                this.$notify({
                    title: '提示',
                    message: '请联系管理员添加权限',
                    type: 'warning',
                    duration: 2000
                });
                return false;
            }
            this.$zdConfirm('确定是否删除该已下架任务？', '删除项目', {
                confirmButtonText: '确认删除',
                cancelButtonText: '取消',
                type: 'danger-warning'
            }).then(() => {
                this.$post('/student/task_stuagent_shop_project%5Cdelete', {
                    shop_project_no: row.shop_project_no
                }, res => {
                    if (res.code === 1) {
                        this.$notify({
                            title: '提示',
                            message: '删除成功',
                            type: 'success'
                        });
                        this.getTableData();
                    } else {
                        this.$notify({
                            title: '提示',
                            message: res.msg,
                            type: 'warning'
                        });
                    }
                });
            }).catch(() => {});
        },

        /**
         * 提交数据
         */
        handleSubmit() {
            this.$refs.ruleForm.validate((valid) => {
                if(valid) {
                    let params = deepClone(this.ruleForm);

                    this.$post('/student/task_stuagent_shop_project%5Csave', params, res => {
                        if (res.code === 1) {
                            this.$notify({
                                title: '提示',
                                message: '保存成功',
                                type: 'success'
                            });
                            this.dialog.dialogVisible = false;
                            this.getTableData();
                        } else {
                            this.$notify({
                                title: '提示',
                                message: res.msg,
                                type: 'warning'
                            });
                        }
                    });
                } else {
                    // 校验不通过
                    if(document.getElementsByClassName('el-form-item__error').length > 0){
                        this.$notify.warning({
                            title: '提示',
                            message: document.getElementsByClassName('el-form-item__error')[0].innerText
                        });
                    }
                    return false;
                }
            });
        },

        /**
         * 弹窗关闭
         */
        handleDialogClose() {

        }
    }
};
</script>

<style lang="less" scoped>
.wrapper {
    width: 100%;

    .content-wrapper {
        display: flex;
        height: calc(100% - 24px);
        flex-direction: column;
        position: relative;
        margin: 12px;
        background: #fff;
        box-sizing: border-box;

        /*header*/

        .header {
            height: 68px;
            padding: 24px;
            box-sizing: border-box;
            box-shadow: 0px -1px 0px 0px #F0F2F5;
            display: flex;
            align-items: center;
            justify-content: space-between;
            border-bottom: 1px solid #F0F2F5;

            .header-title {
                color: #333333;
                font-size: 20px;
                height: 20px;
                font-weight: 500;
                position: relative;
                margin: 24px 0 24px 12px;

                &::before {
                    content: '';
                    position: absolute;
                    left: -8px;
                    top: -1px;
                    width: 3px;
                    height: 20px;
                    background: #1890FF;
                }
            }
        }

        /*筛选*/

        .filter-view {
            box-sizing: border-box;
            display: flex;
            align-items: center;
            justify-content: space-between;
            padding: 0 24px;
            margin-top: 24px;
            .filter-left {
                display: flex;
                align-items: center;
                flex-wrap: wrap;
            }
            .filter-right {
                display: flex;
            }

            .filter-item {
                display: flex;
                align-items: center;
                font-family: PingFangSC-Regular, PingFang SC;
                font-weight: 400;
                color: #333333;
                white-space: nowrap;
                margin-right: 24px;
                margin-bottom: 24px;

                &__title {
                    margin-right: 16px;
                    white-space: nowrap;
                }
                .query-button  {
                    width: 84px;
                    height: 36px;
                    background: #FFFFFF;
                    border-radius: 4px;
                    border: 1px solid #1890FF;
                    font-size: 14px;
                    font-family: PingFangSC-Medium, PingFang SC;
                    font-weight: 500;
                    color: #1890FF;
                    margin-right: 12px;
                }
            }
        }

        // 表格
        .table-wrapper {
            padding: 0 24px;
            flex: 1;
            height: 0;
            .project-logo {
                width: 40px;
                height: 40px;
                object-fit: cover;
            }
        }
        .data-table {
            ::-webkit-scrollbar {
                width: 6px;
                height: 6px;
            }
            // 操作按钮
            .option-btn-group {
                .el-button {
                    margin-left: 0;
                    margin-right: 10px;
                }
            }
        }
        .status-wrapper {
            display: flex;
            align-items: center;
            justify-content: center;

            .status-dot {
                width: 6px;
                height: 6px;
                border-radius: 50%;
                margin-right: 5px;

                &.success {
                    background: #4CD964;
                }

                &.danger {
                    background: #FF3030;
                }

                &.warning {
                    background: #FFA72D;
                }

                &.normal {
                    background: #A6A6A6;
                }
            }
        }

        /* 分页 */
        .pagination-wrapper {
            padding: 12px 24px;
            .el-pagination {
                float: right;
                padding: 0;
                background-color: transparent;
            }
        }
    }
    .dialog-content {
        /deep/ .el-form {
            .is-required {
                .el-form-item__label {
                    &:before {
                        content: "*";
                        display: inline-block;
                        color: #F56C6C;
                    }
                }
            }
        }
        .form-tips {
            font-size: 12px;
            font-family: PingFangSC-Regular, PingFang SC;
            font-weight: 400;
            color: #999999;
            margin-left: 12px;
        }
    }
    .dialog-footer {
        display: flex;
        justify-content: center;
        margin-top: 30px;
    }
    /deep/ .el-dialog__header {
        padding-top: 10px;
        box-shadow:0 1px 0 0 rgba(233,233,233,1);
        display: flex;
        align-items: center;
        background: #FAFAFA;
        border-radius: 4px 4px 0 0;
        .el-dialog__title {
            font-weight: 500;
            color: #333;
            font-size: 14px;
        }
        .el-icon-close:before {
            color: rgb(194,194,194);
            font-size: 17px;
            font-weight: bold;
        }
        .el-dialog__headerbtn {
            top: 14px;
        }
    }
    /deep/ .el-dialog__body {
        padding: 32px 64px 32px 47px;
    }
    /deep/ .el-textarea__inner {
        resize: none;
    }
    /deep/ .el-table__fixed-right {
        right: 6px!important;
    }
    /deep/ .el-radio-button {
        &.is-active {
            .el-radio-button__inner {
                background: #E6F1FC;
                color: #20A0FF;
                border-color: #20A0FF;
            }
        }
        .el-radio-button__inner {
            padding: 10px 24px;
        }
    }
    /deep/ .el-input-number .el-input__inner {
        text-align: left;
    }
    /deep/ .pic-view {
        display: flex;
        align-items: flex-end;

        .tips {
            margin-bottom: 40px;
        }
        .pic-item {
            margin-bottom: 0!important;
        }
        .uploader-customer {
            background: #fff!important;
        }
    }
    /deep/ .el-table {
        th {
            background: #f2f2f2!important;
            font-size: 14px!important;
            font-family: PingFangSC-Medium, PingFang SC!important;
            font-weight: 500!important;
            color: #333333!important;
            height: 44px!important;
        }
    }
}
</style>
